package com.danke.lib.pay;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.util.AttributeSet;
import android.webkit.JsPromptResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * @author wupuquan
 * @version 1.0
 * @since 2019/1/7 20:36
 */
public class DKPayWebView extends WebView {

    public DKPayWebView(Context context) {
        super(context);
        init();
    }

    public DKPayWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DKPayWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        initWebViewSettings();
//        setWebChromeClient(new DKPayWebChromeClient());
//        setWebViewClient(new DKPayWebViewClient());
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initWebViewSettings() {
        WebSettings webSetting = this.getSettings();
        // 设置JS支持
        webSetting.setJavaScriptEnabled(true);
        // 设置可以支持缩放
//		webSetting.setSupportZoom(true);
//		// 设置出现缩放工具
//		webSetting.setBuiltInZoomControls(true);
        // 自适应屏幕
        webSetting.setUseWideViewPort(true);
        webSetting.setLoadWithOverviewMode(true);
        // 支持通过JS打开新窗口
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        // 不进行缓存，总是使用网络数据
//        webSetting.setCacheMode(webSetting.LOAD_NO_CACHE);
        // 允许跨域，http://blog.csdn.net/zhangcanyan/article/details/51930775
        webSetting.setAllowFileAccess(true);
        // 现在遇到个问题：我要报销h5页面里有选图组件，但是显示不出来
        // 看log是因为localStorage访问失败，打开下边这三个就可以了
        webSetting.setAppCacheEnabled(true);
        webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setGeolocationEnabled(true);
        // 播放网络视频需要下面的代码
        webSetting.setPluginState(WebSettings.PluginState.ON);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSetting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webSetting.setAllowContentAccess(true);
        webSetting.setSavePassword(true);
        webSetting.setSaveFormData(true);
    }

    @Override
    public void onResume() {
        try {
            super.onResume();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onPause() {
        try {
            super.onPause();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void destroy() {
        try {
            super.destroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class DKPayWebChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
        }

        @Override
        public boolean onJsPrompt(WebView view, String url, String message,
                                  String defaultValue, JsPromptResult result) {
            return super.onJsPrompt(view, url, message, defaultValue, result);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {

        }
    }

    public static class DKPayWebViewClient extends WebViewClient {
        @Override
        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
        }

        @Override
        public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
            super.doUpdateVisitedHistory(view, url, isReload);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }
    }
}
